/*--------------------------------------------------------------------------------
	DESCRIPTION: Cleaning Gallup 2025

--------------------------------------------------------------------------------*/

clear
import delimited "$data_dir/raw/2025_02_21_gallup_world_poll.csv", encoding(UTF-8)

rename *, upper
keep WAVE WGT COUNTRY_ISO2 COUNTRY_ISO3 YEAR_CALENDAR YEAR_WAVE WP139 WP138 WP112 WP1718 WP1717 WP9039 WP137 WP142 WP146 WP10251 WP2319 WP30 WP108 WP109 WP110 WP27 WP1220 WP1219 WP1233RECODED WP3117 EMP_2010 INCOME_2 INCOME_5 WP1223 WP7572 WP4657

compress

*********************************************
*************SURVEY VARIABLES VALUES*********
*********************************************

rename COUNTRY_ISO3 ccode
clonevar S009 = COUNTRY_ISO2

merge m:1 S009 using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop if _merge==1
drop _merge

gen iso2 = S009

gen survey = "Gallup"

rename WAVE wave_gallup
gen wave = floor(wave_gallup)

gen weight = WGT

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if WP139==1
replace trust_govt = 0 if WP139==2

gen trust_govt_dk_ref = 1 if WP139==3|WP139==4 //"don't know" or "refused" option
replace trust_govt_dk_ref = 0 if trust_govt_dk_ref==. & WP139!=. 

gen trust_justice = 1 if WP138==1
replace trust_justice = 0 if WP138==2

gen trust_police = 1 if WP112==1
replace trust_police = 0 if WP112==2

gen trust_civil_service = 1 if WP1718==1
replace trust_civil_service = 0 if WP1718==2

gen trust_local_govt = 1 if WP1717==1
replace trust_local_govt = 0 if WP1717==2

gen trust_parties = .

gen trust_parliament = .

gen trust_others = 1 if WP9039==1
replace trust_others = 0 if WP9039==2 

gen trust_military = 1 if WP137==1
replace trust_military = 0 if WP137==2

gen trust_religious_organizations = 1 if WP142==1
replace trust_religious_organizations = 0 if WP142==2

pca trust_govt trust_justice trust_police, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

gen govt_corrupt = 1 if WP146==1
replace govt_corrupt = 0 if WP146==2

gen media_free = 1 if WP10251==1
replace media_free = 0 if WP10251==2

****************************************
*************ECONOMY CONFIDENCE*********
****************************************

gen feelings_income = 1 if inlist(WP2319, 1, 2)
replace feelings_income = 0 if inlist(WP2319, 3, 4)

gen standard_living = 1 if WP30==1
replace standard_living = 0 if WP30==2

**********************************************
*****************SOCIAL CAPITAL***************
**********************************************

gen donate_money = 1 if WP108==1
replace donate_money = 0 if WP108==2

gen volunteered_time = 1 if WP109==1
replace volunteered_time = 0 if WP109==2

gen help_stranger = 1 if WP110==1
replace help_stranger = 0 if WP110==2

gen relatives_help = 1 if WP27==1
replace relatives_help = 0 if WP27==2

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = YEAR_CALENDAR
gen age = WP1220
replace age = . if age==100
gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			1   Male               
*			2   Female                                
*		  -99   Missing

gen sex = 1 if WP1219==1
replace sex = 2 if WP1219==2
replace sex = -99 if WP1219==.

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if WP1233RECODED==6
replace religion = 2 if WP1233RECODED==4
replace religion = 3 if WP1233RECODED==5
replace religion = 4 if WP1233RECODED==1
replace religion = 5 if WP1233RECODED==2
replace religion = 6 if WP1233RECODED==7
replace religion = 7 if WP1233RECODED==3
replace religion = -99 if WP1233RECODED==8
replace religion = -99 if religion==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if WP3117==1
replace edu = 2 if WP3117==2
replace edu = 3 if WP3117==3
replace edu = -99 if WP3117==4|WP3117==5

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = -99

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = . 
replace employment_status = 1 if EMP_2010==1|EMP_2010==2|EMP_2010==3|EMP_2010==5
replace employment_status = 2 if EMP_2010==4|EMP_2010==6
replace employment_status = -99 if employment_status==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = INCOME_5 //income bands
replace income = -99 if INCOME_5==.

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = .
replace marital = 1 if WP1223==2|WP1223==8
replace marital = 2 if WP1223==1|WP1223==3|WP1223==4|WP1223==5
replace marital = -99 if marital==.

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

clonevar urban = WP7572
replace urban = -99 if urban==.

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = 1 if WP4657==1
replace born_country = 0 if WP4657==2

keep ccode S009 country numcode country_short cow iso2 survey wave weight trust_govt trust_govt_dk_ref trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations trust_state govt_corrupt media_free feelings_income standard_living donate_money volunteered_time help_stranger relatives_help survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban born_country

sort ccode survey_year
order ccode survey_year

save "$data_dir/clean/gallup_clean.dta", replace

